<?php
function settings() {
	global $db;
	
	$settings = array(
		'landing',
		'faq',
		'aboutus',
		'contactus',
		'event',
		'category',
		'other',
	);
	$r = submenu_show($settings, 'settings');
	return $r;
}

function settings_other(){
	global $dir_folder;
	$dir = $dir_folder['other'];//'im/landing/';

	if ($_POST['save']) {
		if ($_FILES['main_background_img']) {
			$extension = fw_file_extension($_FILES['main_background_img']['name']);
			if ($extension == 'jpg' || $extension == 'jpeg') {
				$name = $dir . 'bg.jpg';
				move_uploaded_file($_FILES['main_background_img']['tmp_name'], $name);
			} else {
				$opt['missing_field'] = 'for background image allow only jpg OR jpeg file';
				echo json_encode($opt);
				exit;
			}
		}
		
		if ($_FILES['faq_img']) {
			$extension = fw_file_extension($_FILES['faq_img']['name']);
			if ($extension == 'jpg' || $extension == 'jpeg') {
				$name = $dir . 'faq.jpg';
				move_uploaded_file($_FILES['faq_img']['tmp_name'], $name);
			} else {
				$opt['missing_field'] = 'for FAQ image allow only jpg OR jpeg file';
				echo json_encode($opt);
				exit;
			}
		}
		
		if ($_FILES['logo_img']) {
			$extension = fw_file_extension($_FILES['logo_img']['name']);
			if ($extension == 'png') {
				$name = $dir . 'logo.png';
				move_uploaded_file($_FILES['logo_img']['tmp_name'], $name);
			} else {
				$opt['missing_field'] = 'for LOGO image allow only jpg OR jpeg file';
				echo json_encode($opt);
				exit;
			}
		}
		
		$opt['completed'] = 1; 
		echo json_encode($opt);
		exit;
	}
	
	if (!$_REQUEST['id'] && !$_REQUEST['link']) {
		$_REQUEST['id'] = 1;
		$_REQUEST['link'] = 'edit';
	}

	if ($_REQUEST['link'] == 'edit' || $_REQUEST['link'] == 'add') {
		$content = settings_other_form();
	} else {
		$content = settings_other_list();
	}
	
	$r = style_page1('Other', $content).'
	<script type="text/javascript">
		$(document).ready(function(){
			page_style();
		});
	</script>';
	return settings().$r;
}

function settings_other_form() {
	global $dir_folder;
	$dir = $dir_folder['other'];//'im/landing/';

	$bg_file = $dir . 'bg.jpg';
	$show_bg_file = '';
	if (file_exists($bg_file)){
		$show_bg_file .= '<img src="'.$bg_file.'" width="200" style="border: 1px solid #999;"/>';
	}

	$faq_file = $dir . 'faq.jpg';
	$show_faq_file = '';
	if (file_exists($faq_file)){
		$show_faq_file .= '<img src="'.$faq_file.'" width="200" style="border: 1px solid #999;"/>';
	}

	$logo_file = $dir . 'logo.png';
	$show_logo_file = '';
	if (file_exists($logo_file)){
		$show_logo_file .= '<img src="'.$logo_file.'" width="200" style="border: 1px solid #999;"/>';
	}
	$r .= '<form id="form_settings" method="post" action="index.php?mod=settings&view=other&ajax=1" enctype="multipart/form-data">
			<table style="float: left;">
				<tr><td>Img-Background*:<br/>'.form_file('main_background_img', array()).'<br/>'.$show_bg_file.'</td></tr>
				<tr><td>Img-FAQ*[350x400]:<br/>'.form_file('faq_img', array()).'<br/>'.$show_faq_file.'</td></tr>
				<tr><td>Img-Logo*[150x32]:<br/>'.form_file('logo_img', array()).'<br/>'.$show_logo_file.'</td></tr>
				<tr><td align="right">
				<input type="hidden" name="id" value="'.$_REQUEST['id'].'"/>
					<input type="button" name="cancel" value="cancel" onclick="window.location=\'index.php?mod=settings&view=other\'"/>
					<input type="submit" name="save" value="save"/>
				</td></tr>
			</table>
		</form>
		<script type="text/javascript">
			$(document).ready(function(){
				$("input[type=submit], input[type=button]").button();
				$("#form_settings").ajaxForm({
					dataType:  "json",
					success:   processJson
				});
			});
			function processJson(data) {
				if (data.completed == 1) {
					alert("updated");
					window.location="index.php?mod=settings&view=other";
				} else {
					alert(data.missing_field);
				}
			}
		</script>
		';
	return $r;
}


function settings_other_list(){
	return '';
}

function settings_landing(){
	global $dir_folder;
	$dir = $dir_folder['landing'];//'im/landing/';
	
	if ($_REQUEST['link'] == 'delete') {
		$param_sql = array(
			'table' => 'sf_landing',
			'cols' => array(
				'removed', '', 'NOW()',
				'removed_by', '%d', $_SESSION[SESSION_L]
			),
			'where' => SQL('id_landing=%d', $_REQUEST['id']),
		);
		db_query($param_sql);
	}

	if ($_POST['save']) {
		
		$missing_field = 0;
		if (trim($_POST['landing_header']) == '') {
			++$missing_field;
		}
		if (trim($_POST['landing_details']) == '') {
			++$missing_field;
		}
		if (trim($_POST['landing_link']) == '') {
			++$missing_field;
		}
		if ($missing_field > 0) {
			$opt['missing_field'] = 'Missing Required Fields';
			echo json_encode($opt);
			exit;
		}

		$param_sql = array(
			'table' => 'sf_landing',
			'cols' => array(
				'landing_header', '%s', $_POST['landing_header'],
				'landing_details', '%s', $_POST['landing_details'],
				'landing_link', '%s', $_POST['landing_link'],
				'updated', '', 'NOW()',
				'updated_by', '%d', $_SESSION[SESSION_L]
			),
		);
		
		if ($_FILES['landing_img']) {
			$extension = fw_file_extension($_FILES['landing_img']['name']);
			$param_sql['cols'][] = 'landing_img';
			$param_sql['cols'][] = '%s';
			$param_sql['cols'][] = $extension;
		}

		if ($_POST['id'] > 0) {
			$id_landing = $_POST['id'];
			//edit
			$param_sql['where'] = SQL('id_landing=%d', $id_landing);
			db_query($param_sql);
		} else {
			//add new
			$param_sql['cols'][] = 'added';
			$param_sql['cols'][] = '';
			$param_sql['cols'][] = 'NOW()';
			$param_sql['cols'][] = 'added_by';
			$param_sql['cols'][] = '%d';
			$param_sql['cols'][] = $_SESSION[SESSION_L];
			$id_landing = db_query($param_sql);
		}

		if ($_FILES['landing_img']) {
			$name = $dir . $id_landing.'.'.$extension;
			move_uploaded_file($_FILES['landing_img']['tmp_name'], $name);
		}
		
		$opt['completed'] = 1; 
		echo json_encode($opt);
		exit;
	}
	
	if ($_REQUEST['link'] == 'edit' || $_REQUEST['link'] == 'add') {
		$content = settings_landing_form();
	} else {
		$content = settings_landing_list();
	}
	
	$r = '
	<div id="page">
		'.bg_page().'
		<div id="page-title" style="margin-top: -200px; transition: all 2s cubic-bezier(0.25, 0.1, 0.25, 1) 2s;">
			Landing
		</div>
		<div class="settings" id="page-content" style="width: 934px; left: -100px; opacity: 0; transition: all 2s cubic-bezier(0.25, 0.1, 0.25, 1) 0s ;">
			<div id="page-text" style="opacity: 1; transition: all 0s cubic-bezier(0.25, 0.1, 0.25, 1) 0s ;">
				'.$content.'
			</div>
		</div>
	</div>
	<script type="text/javascript">
		$(document).ready(function(){
			page_style();
		});
	</script>';
	return settings().$r;
}

function settings_landing_form() {
	global $db, $dir_folder;
	$dir = $dir_folder['landing'];//'im/landing/';
	if ($_REQUEST['link'] == 'edit') {
		$sql = SQL('
		SELECT
			landing_header, 
			landing_details, 
			landing_link, 
			landing_img, 
			added, 
			added_by, 
			updated, 
			updated_by, 
			removed, 
			removed_by
		FROM 
			sf_landing
		WHERE 
			removed IS NULL
			AND id_landing=%d', $_REQUEST['id']);
		$row = $db->GetRow($sql);
	
		$im_file = $dir . $_REQUEST['id'] . '.' . $row['landing_img'];
		$show_im_file = '';
		if (file_exists($im_file)){
			$show_im_file .= '<img src="'.$im_file.'" width="200" style="border: 1px solid #999; margin-top: 10px;"/>';
		}
	}
	
	$r .= '<form id="form_settings" method="post" action="index.php?mod=settings&view=landing&ajax=1" enctype="multipart/form-data">
			<table style="float: left;">
				<tr><td>Header*:<br/>'.form_input('landing_header', array('default' => $row['landing_header'], 'param1' => 300)).'</td></tr>
				<tr><td>Img-Background*:<br/>'.form_file('landing_img', array()).'<br/>'.$show_im_file.'</td></tr>
				<tr><td>Link*:<br/>'.form_input('landing_link', array('default' => $row['landing_link'], 'param1' => 300)).'</td></tr>
				<tr><td>Details*:<br/>'.form_textarea('landing_details', array('default' => $row['landing_details'])).'</td></tr>
				<tr><td align="right">
				<input type="hidden" name="id" value="'.$_REQUEST['id'].'"/>
					<input type="button" name="cancel" value="cancel" onclick="window.location=\'index.php?mod=settings&view=landing\'"/>
					<input type="submit" name="save" value="save"/>
				</td></tr>
			</table>
		</form>
		<script type="text/javascript">
			$(document).ready(function(){
				$("input[name=save]").click(function(){
					$("textarea[name=landing_details]").val(tinyMCE.get("landing_details").getContent());
				});
				$("input[type=submit], input[type=button]").button();
				$("#form_settings").ajaxForm({
					dataType:  "json",
					success:   processJson
				});
			});
			function processJson(data) {
				if (data.completed == 1) {
					alert("updated");
					window.location="index.php?mod=settings&view=landing";
				} else {
					alert(data.missing_field);
				}
			}
		</script>
		';
	return $r;
}

function settings_landing_list() {
	global $db;
	$sql = '
	SELECT
		id_landing, 
		landing_header, 
		landing_details, 
		landing_link,
		landing_img, 
		added, 
		added_by, 
		updated, 
		updated_by, 
		removed, 
		removed_by
	FROM 
		sf_landing
	WHERE 
		removed IS NULL';
	$rsl = $db->GetAssoc($sql);
	$tbody = '';
	if ($rsl) {
		foreach ($rsl as $k => $v) {
			$tbody .= '
			<tr class="item'.(++$j%2).'">
				<td align="center">'.(++$i).'</td>
					<td>'.$v['landing_header'].'</td>
					<td>'.$v['landing_details'].'</td>
					<td align="center">
						<a href="index.php?mod=settings&view=landing&link=edit&id='.$k.'">edit</a> 
						<a href="index.php?mod=settings&view=landing&link=delete&id='.$k.'" onclick="return confirm_remove();">delete</a>
					</td>
			</tr>';
		}
	}
	
	$r .= '<a href="index.php?mod=settings&view=landing&link=add">new</a>
	<table width="95%" class="settings_listing" cellpadding="4" cellspacing="0">
			<thead><tr><th width="30"></th><th width="200">Header</th><th>Details</th><th width="150">Option</th></tr></thead>
			'.$tbody.'
		</table>';
	return $r;
}

function settings_faq(){
	global $dir_folder;
	$dir = $dir_folder['faq'];//'im/landing/';
	
	if ($_REQUEST['link'] == 'delete') {
		$param_sql = array(
			'table' => 'sf_faq',
			'cols' => array(
				'removed', '', 'NOW()',
				'removed_by', '%d', $_SESSION[SESSION_L]
			),
			'where' => SQL('id_faq=%d', $_REQUEST['id']),
		);
		db_query($param_sql);
	}

	if ($_POST['save']) {
		$missing_field = 0;
		if (trim($_POST['faq_question']) == '') {
			++$missing_field;
		}
		if (trim($_POST['faq_answer']) == '') {
			++$missing_field;
		}
		if ($missing_field > 0) {
			$opt['missing_field'] = 'Missing Required Fields';
			echo json_encode($opt);
			exit;
		}

		$param_sql = array(
			'table' => 'sf_faq',
			'cols' => array(
				'faq_question', '%s', $_POST['faq_question'],
				'faq_answer', '%s', $_POST['faq_answer'],
				'updated', '', 'NOW()',
				'updated_by', '%d', $_SESSION[SESSION_L]
			),
		);
		
// 		if ($_FILES['landing_img']) {
// 			$extension = fw_file_extension($_FILES['landing_img']['name']);
// 			$param_sql['cols'][] = 'landing_img';
// 			$param_sql['cols'][] = '%s';
// 			$param_sql['cols'][] = $extension;
// 		}

		if ($_POST['id'] > 0) {
			$id_faq = $_POST['id'];
			//edit
			$param_sql['where'] = SQL('id_faq=%d', $id_faq);
			db_query($param_sql);
		} else {
			//add new
			$param_sql['cols'][] = 'added';
			$param_sql['cols'][] = '';
			$param_sql['cols'][] = 'NOW()';
			$param_sql['cols'][] = 'added_by';
			$param_sql['cols'][] = '%d';
			$param_sql['cols'][] = $_SESSION[SESSION_L];
			$id_faq = db_query($param_sql);
		}

// 		if ($_FILES['landing_img']) {
// 			$name = $dir . $id_faq.'.'.$extension;
// 			move_uploaded_file($_FILES['landing_img']['tmp_name'], $name);
// 		}
		
		$opt['completed'] = 1; 
		echo json_encode($opt);
		exit;
	}
	
	if ($_REQUEST['link'] == 'edit' || $_REQUEST['link'] == 'add') {
		$content = settings_faq_form();
	} else {
		$content = settings_faq_list();
	}
	$r = '
	<div id="page">
		'.bg_page().'
		<div id="page-title" style="margin-top: -200px; transition: all 2s cubic-bezier(0.25, 0.1, 0.25, 1) 2s;">
			FAQ
		</div>
		<div class="settings" id="page-content" style="width: 934px; left: -100px; opacity: 0; transition: all 2s cubic-bezier(0.25, 0.1, 0.25, 1) 0s ;">
			<div id="page-text" style="opacity: 1; transition: all 0s cubic-bezier(0.25, 0.1, 0.25, 1) 0s ;">
				'.$content.'
			</div>
		</div>
	</div>
	<script type="text/javascript">
		$(document).ready(function(){
			page_style();
		});
	</script>';
	return settings().$r;
}

function settings_faq_form() {
	global $db;
	if ($_REQUEST['link'] == 'edit') {
		$sql = SQL('
		SELECT 	
			id_faq, 
			faq_question, 
			faq_answer, 
			faq_order, 
			added, 
			added_by, 
			updated, 
			updated_by, 
			removed, 
			removed_by
		FROM 
			sf_faq 
		WHERE 
			removed IS NULL
			AND id_faq=%d', $_REQUEST['id']);
		$row = $db->GetRow($sql);
	}
	
	$r .= '<form id="form_settings" method="post" action="index.php?mod=settings&view=faq&ajax=1" enctype="multipart/form-data">
			<table style="float: left;">
				<tr><td>Question*:<br/>'.form_input('faq_question', array('default' => $row['faq_question'], 'param1' => 300)).'</td></tr>
				<tr><td>Answer*:<br/>'.form_textarea('faq_answer', array('default' => $row['faq_answer'])).'</td></tr>
				<tr><td align="right"><input type="hidden" name="id" value="'.$_REQUEST['id'].'"/>
					<input type="button" name="cancel" value="cancel" onclick="window.location=\'index.php?mod=settings&view=faq\'"/>
					<input type="submit" name="save" value="save"/></td></tr>
			</table>
		</form>
		<script type="text/javascript">
			$(document).ready(function(){
				$("input[type=submit], input[type=button]").button();
				$("#form_settings").ajaxForm({
					dataType:  "json",
					success:   processJson
				});
			});
			
			function processJson(data) {
				if (data.completed == 1) {
					alert("updated");
					window.location="index.php?mod=settings&view=faq";
				} else {
					alert(data.missing_field);
				}
			}
		</script>
		';
	return $r;
}

function settings_faq_list() {
	global $db;
	$sql = '
	SELECT 	
		id_faq, 
		faq_question, 
		faq_answer, 
		faq_order, 
		added, 
		added_by, 
		updated, 
		updated_by, 
		removed, 
		removed_by
	FROM 
		sf_faq
	WHERE 
		removed IS NULL';
	$rsl = $db->GetAssoc($sql);
	$tbody = '';
	if ($rsl) {
		foreach ($rsl as $k => $v) {
			$tbody .= '
			<tr class="item'.(++$j%2).'">
				<td align="center">'.(++$i).'</td>
					<td>'.$v['faq_question'].'</td>
					<td>'.$v['faq_answer'].'</td>
					<td align="center">
						<a href="index.php?mod=settings&view=faq&link=edit&id='.$k.'">edit</a> 
						<a href="index.php?mod=settings&view=faq&link=delete&id='.$k.'" onclick="return confirm_remove();">delete</a>
					</td>
			</tr>';
		}
	}
	
	$r .= '<a href="index.php?mod=settings&view=faq&link=add">new</a>
	<table width="95%" class="settings_listing" cellpadding="4" cellspacing="0">
			<thead><tr><th width="30"></th><th>Question</th><th>Answer</th><th width="150">Option</th></tr></thead>
			'.$tbody.'
		</table>';
	return $r;
}

function settings_category(){	
	global $dir_folder;
	$dir = $dir_folder['category'];//'im/landing/';
	
	if ($_REQUEST['link'] == 'delete') {
		$param_sql = array(
			'table' => 'sf_category',
			'cols' => array(
				'removed', '', 'NOW()',
				'removed_by', '%d', $_SESSION[SESSION_L]
			),
			'where' => SQL('id_category=%d', $_REQUEST['id']),
		);
		db_query($param_sql);
	}

	if ($_POST['save']) {
		$missing_field = 0;
		if (trim($_POST['category_name']) == '') {
			++$missing_field;
		}
		if (trim($_POST['category_detail1']) == '') {
			++$missing_field;
		}
		if ($missing_field > 0) {
			$opt['missing_field'] = 'Missing Required Fields';
			echo json_encode($opt);
			exit;
		}

		$param_sql = array(
			'table' => 'sf_category',
			'cols' => array(
				'category_name', '%s', $_POST['category_name'],
				'category_detail1', '%s', $_POST['category_detail1'],
				'category_detail2', '%s', $_POST['category_detail2'],
				'updated', '', 'NOW()',
				'updated_by', '%d', $_SESSION[SESSION_L]
			),
		);
		
		if ($_FILES['category_img']) {
			$extension = fw_file_extension($_FILES['category_img']['name']);
			$param_sql['cols'][] = 'category_img';
			$param_sql['cols'][] = '%s';
			$param_sql['cols'][] = $extension;
		}
		
		if ($_FILES['category_img1']) {
			$extension1 = fw_file_extension($_FILES['category_img1']['name']);
			$param_sql['cols'][] = 'category_img1';
			$param_sql['cols'][] = '%s';
			$param_sql['cols'][] = $extension1;
		}
		
		if ($_FILES['category_img2']) {
			$extension2 = fw_file_extension($_FILES['category_img2']['name']);
			$param_sql['cols'][] = 'category_img2';
			$param_sql['cols'][] = '%s';
			$param_sql['cols'][] = $extension2;
		}

		if ($_POST['id'] > 0) {
			$id_landing = $_POST['id'];
			//edit
			$param_sql['where'] = SQL('id_category=%d', $id_landing);
			db_query($param_sql);
		} else {
			//add new
			$param_sql['cols'][] = 'added';
			$param_sql['cols'][] = '';
			$param_sql['cols'][] = 'NOW()';
			$param_sql['cols'][] = 'added_by';
			$param_sql['cols'][] = '%d';
			$param_sql['cols'][] = $_SESSION[SESSION_L];
			$id_landing = db_query($param_sql);
		}

		if ($_FILES['category_img']) {
			$name = $dir . $id_landing.'.'.$extension;
			move_uploaded_file($_FILES['category_img']['tmp_name'], $name);
		}

		if ($_FILES['category_img1']) {
			$name = $dir . 'detail1_' . $id_landing.'.'.$extension1;
			move_uploaded_file($_FILES['category_img1']['tmp_name'], $name);
		}

		if ($_FILES['category_img2']) {
			$name = $dir . 'detail2_' . $id_landing.'.'.$extension2;
			move_uploaded_file($_FILES['category_img2']['tmp_name'], $name);
		}
		
		$opt['completed'] = 1; 
		echo json_encode($opt);
		exit;
	}
	
	if ($_REQUEST['link'] == 'edit' || $_REQUEST['link'] == 'add') {
		$content = settings_category_form();
	} else {
		$content = settings_category_list();
	}
	$r = '
	<div id="page">
		'.bg_page().'
		<div id="page-title" style="margin-top: -200px; transition: all 2s cubic-bezier(0.25, 0.1, 0.25, 1) 2s;">
			Category
		</div>
		<div class="settings" id="page-content" style="width: 934px; left: -100px; opacity: 0; transition: all 2s cubic-bezier(0.25, 0.1, 0.25, 1) 0s ;">
			<div id="page-text" style="opacity: 1; transition: all 0s cubic-bezier(0.25, 0.1, 0.25, 1) 0s ;">
				'.$content.'
			</div>
		</div>
	</div>
	<script type="text/javascript">
		$(document).ready(function(){
			page_style();
		});
	</script>';
	return settings().$r;
}

function settings_category_form() {
	global $db, $dir_folder;
	$dir = $dir_folder['category'];//'im/landing/';
	if ($_REQUEST['link'] == 'edit') {
		$sql = SQL('
		SELECT
			category_name, 
			category_img, 
			category_detail1, 
			category_img1, 
			category_detail2,
			category_img2, 
			added, 
			added_by, 
			updated, 
			updated_by, 
			removed, 
			removed_by
		FROM 
			sf_category
		WHERE 
			removed IS NULL
			AND id_category=%d', $_REQUEST['id']);
		$row = $db->GetRow($sql);
	
		$im_file = $dir . $_REQUEST['id'] . '.' . $row['category_img'];
		$show_im_file = '';
		if (file_exists($im_file)){
			$show_im_file .= '<img src="'.$im_file.'" width="200" style="border: 1px solid #999; margin-top: 10px;"/>';
		}
	
		$im_file1 = $dir . 'detail1_' . $_REQUEST['id'] . '.' . $row['category_img1'];
		$show_im_file1 = '';
		if (file_exists($im_file1)){
			$show_im_file1 .= '<img src="'.$im_file1.'" width="200" style="border: 1px solid #999; margin-top: 10px;"/>';
		}
	
		$im_file2 = $dir . 'detail2_' . $_REQUEST['id'] . '.' . $row['category_img2'];
		$show_im_file2 = '';
		if (file_exists($im_file2)){
			$show_im_file2 .= '<img src="'.$im_file2.'" width="200" style="border: 1px solid #999; margin-top: 10px;"/>';
		}
	}
	
	$r .= '<form id="form_settings" method="post" action="index.php?mod=settings&view=category&ajax=1" enctype="multipart/form-data">
			<table style="float: left;">
				<tr><td>Name*:<br/>'.form_input('category_name', array('default' => $row['category_name'], 'param1' => 300)).'</td></tr>
				<tr><td>Img-Category*:<br/>'.form_file('category_img', array()).'<br/>'.$show_im_file.'</td></tr>
				<tr><td>Img-Category details1*[600*200]:<br/>'.form_file('category_img1', array()).'<br/>'.$show_im_file1.'</td></tr>
				<tr><td>Details1*:<br/>'.form_textarea('category_detail1', array('default' => $row['category_detail1'])).'</td></tr>
				<tr><td>Img-Category details2*[600*200]:<br/>'.form_file('category_img2', array()).'<br/>'.$show_im_file2.'</td></tr>
				<tr><td>Details2:<br/>'.form_textarea('category_detail2', array('default' => $row['category_detail2'])).'</td></tr>
				<tr><td align="right"><input type="hidden" name="id" value="'.$_REQUEST['id'].'"/>
					<input type="button" name="cancel" value="cancel" onclick="window.location=\'index.php?mod=settings&view=category\'"/>
					<input type="submit" name="save" value="save"/></td></tr>
			</table>
		</form>
		<script type="text/javascript">
			$(document).ready(function(){
				$("#page-content").css("height", "650px");
				$("input[name=save]").click(function(){
					$("textarea[name=category_detail1]").val(tinyMCE.get("category_detail1").getContent());
					$("textarea[name=category_detail2]").val(tinyMCE.get("category_detail2").getContent());
				});
				$("input[type=submit], input[type=button]").button();
				$("#form_settings").ajaxForm({
					dataType:  "json",
					success:   processJson
				});
			});
			
			function processJson(data) {
				if (data.completed == 1) {
					alert("updated");
					window.location="index.php?mod=settings&view=category";
				} else {
					alert(data.missing_field);
				}
			}
		</script>
		';
	return $r;
}

function settings_category_list() {
	global $db;
	$sql = '
	SELECT
		id_category, 
		category_name, 
		category_img, 
		category_detail1, 
		category_detail2,
		added, 
		added_by, 
		updated, 
		updated_by, 
		removed, 
		removed_by
	FROM 
		sf_category
	WHERE 
		removed IS NULL';
	$rsl = $db->GetAssoc($sql);
	$tbody = '';
	if ($rsl) {
		foreach ($rsl as $k => $v) {
			$tbody .= '
			<tr class="item'.(++$j%2).'">
				<td align="center">'.(++$i).'</td>
					<td>'.$v['category_name'].'</td>
					<td align="center">
						<a href="index.php?mod=settings&view=category&link=edit&id='.$k.'">edit</a> 
						<a href="index.php?mod=settings&view=category&link=delete&id='.$k.'" onclick="return confirm_remove();">delete</a>
					</td>
			</tr>';
		}
	}
	
	$r .= '<a href="index.php?mod=settings&view=category&link=add">new</a>
	<table class="settings_listing" cellpadding="4" cellspacing="0" width="95%">
			<thead><tr><th width="30"></th><th>Name</th><th width="150">Option</th></tr></thead>
			'.$tbody.'
		</table>';
	return $r;
}

function settings_event(){	
	global $dir_folder;
	$dir = $dir_folder['event'];//'im/landing/';
	
	if ($_REQUEST['link'] == 'delete') {
		$param_sql = array(
			'table' => 'sf_event',
			'cols' => array(
				'removed', '', 'NOW()',
				'removed_by', '%d', $_SESSION[SESSION_L]
			),
			'where' => SQL('id_event=%d', $_REQUEST['id']),
		);
		db_query($param_sql);
	}

	if ($_POST['save']) {
		$missing_field = 0;
		if (trim($_POST['event_name']) == '') {
			++$missing_field;
		}
		if (trim($_POST['event_detail1']) == '') {
			++$missing_field;
		}
		if ($missing_field > 0) {
			$opt['missing_field'] = 'Missing Required Fields';
			echo json_encode($opt);
			exit;
		}

		$param_sql = array(
			'table' => 'sf_event',
			'cols' => array(
				'event_name', '%s', $_POST['event_name'],
				'event_detail1', '%s', $_POST['event_detail1'],
				'updated', '', 'NOW()',
				'updated_by', '%d', $_SESSION[SESSION_L]
			),
		);
		
		if ($_FILES['event_img']) {
			$extension = fw_file_extension($_FILES['event_img']['name']);
			$param_sql['cols'][] = 'event_img';
			$param_sql['cols'][] = '%s';
			$param_sql['cols'][] = $extension;
		}
		
		if ($_FILES['event_img1']) {
			$extension1 = fw_file_extension($_FILES['event_img1']['name']);
			$param_sql['cols'][] = 'event_img1';
			$param_sql['cols'][] = '%s';
			$param_sql['cols'][] = $extension1;
		}

		if ($_POST['id'] > 0) {
			$id_event = $_POST['id'];
			//edit
			$param_sql['where'] = SQL('id_event=%d', $id_event);
			db_query($param_sql);
		} else {
			//add new
			$param_sql['cols'][] = 'added';
			$param_sql['cols'][] = '';
			$param_sql['cols'][] = 'NOW()';
			$param_sql['cols'][] = 'added_by';
			$param_sql['cols'][] = '%d';
			$param_sql['cols'][] = $_SESSION[SESSION_L];
			$id_event = db_query($param_sql);
		}
		if ($_FILES['event_img']) {
			$name = $dir . $id_event.'.'.$extension;
			move_uploaded_file($_FILES['event_img']['tmp_name'], $name);
		}

		if ($_FILES['event_img1']) {
			$name = $dir . 'detail1_' . $id_event.'.'.$extension1;
			move_uploaded_file($_FILES['event_img1']['tmp_name'], $name);
		}
		
		$opt['completed'] = 1; 
		echo json_encode($opt);
		exit;
	}
	
	if ($_REQUEST['link'] == 'edit' || $_REQUEST['link'] == 'add') {
		$content = settings_event_form();
	} else {
		$content = settings_event_list();
	}
	$r = '
	<div id="page">
		'.bg_page().'
		<div id="page-title" style="margin-top: -200px; transition: all 2s cubic-bezier(0.25, 0.1, 0.25, 1) 2s;">
			event
		</div>
		<div class="settings" id="page-content" style="width: 934px; left: -100px; opacity: 0; transition: all 2s cubic-bezier(0.25, 0.1, 0.25, 1) 0s ;">
			<div id="page-text" style="opacity: 1; transition: all 0s cubic-bezier(0.25, 0.1, 0.25, 1) 0s ;">
				'.$content.'
			</div>
		</div>
	</div>
	<script type="text/javascript">
		$(document).ready(function(){
			page_style();
		});
	</script>';
	return settings().$r;
}

function settings_event_form() {
	global $db, $dir_folder;
	$dir = $dir_folder['event'];//'im/landing/';
	if ($_REQUEST['link'] == 'edit') {
		$sql = SQL('
		SELECT
			event_name, 
			event_img, 
			event_detail1, 
			added, 
			added_by, 
			updated, 
			updated_by, 
			removed, 
			removed_by
		FROM 
			sf_event
		WHERE 
			removed IS NULL
		AND id_event=%d', $_REQUEST['id']);
		$row = $db->GetRow($sql);
	
		$im_file = $dir . $_REQUEST['id'] . '.' . $row['event_img'];
		$show_im_file = '';
		if (file_exists($im_file)){
			$show_im_file .= '<img src="'.$im_file.'" width="200" style="border: 1px solid #999; margin-top: 10px;"/>';
		}
	
		$im_file1 = $dir . 'detail1_' . $_REQUEST['id'] . '.' . $row['event_img'];
		$show_im_file1 = '';
		if (file_exists($im_file1)){
			$show_im_file1 .= '<img src="'.$im_file1.'" width="200" style="border: 1px solid #999; margin-top: 10px;"/>';
		}
	}
	
	$r .= '<form id="form_settings" method="post" action="index.php?mod=settings&view=event&ajax=1" enctype="multipart/form-data">
			<table style="float: left;">
				<tr><td>Name*:<br/>'.form_input('event_name', array('default' => $row['event_name'], 'param1' => 300)).'</td></tr>
				<tr><td>Img-event*:<br/>'.form_file('event_img', array()).'<br/>'.$show_im_file.'</td></tr>
				<tr><td>Img-event1*[350x400]:<br/>'.form_file('event_img1', array()).'<br/>'.$show_im_file1.'</td></tr>
				<tr><td>Details1*:<br/>'.form_textarea('event_detail1', array('default' => $row['event_detail1'])).'</td></tr>
				<tr><td align="right"><input type="hidden" name="id" value="'.$_REQUEST['id'].'"/><input type="submit" name="save" value="save"/></td></tr>
			</table>
		</form>
		<script type="text/javascript">
			$(document).ready(function(){
				$("#page-content").css("height", "650px");
				$("input[name=save]").click(function(){
					$("textarea[name=event_detail1]").val(tinyMCE.get("event_detail1").getContent());
				});
				$("input[type=submit], input[type=button]").button();
				$("#form_settings").ajaxForm({
					dataType:  "json",
					success:   processJson
				});
			});
			
			function processJson(data) {
				if (data.completed == 1) {
					alert("updated");
					window.location="index.php?mod=settings&view=event";
				} else {
					alert(data.missing_field);
				}
			}
		</script>
		';
	return $r;
}

function settings_event_list() {
	global $db;
	$sql = '
	SELECT
		id_event, 
		event_name, 
		event_img, 
		event_detail1, 
		added, 
		added_by, 
		updated, 
		updated_by, 
		removed, 
		removed_by
	FROM 
		sf_event
	WHERE 
		removed IS NULL';
	$rsl = $db->GetAssoc($sql);
	$tbody = '';
	if ($rsl) {
		foreach ($rsl as $k => $v) {
			$tbody .= '
			<tr class="item'.(++$j%2).'">
				<td align="center">'.(++$i).'</td>
					<td>'.$v['event_name'].'</td>
					<td align="center">
						<a href="index.php?mod=settings&view=event&link=edit&id='.$k.'">edit</a> 
						<a href="index.php?mod=settings&view=event&link=delete&id='.$k.'" onclick="return confirm_remove();">delete</a>
					</td>
			</tr>';
		}
	}
	
	$r .= '<a href="index.php?mod=settings&view=event&link=add">new</a><table width="95%" class="settings_listing" cellpadding="4" cellspacing="1">
			<thead><tr><th width="30"></th><th>Name</th><th width="150">Option</th></tr></thead>
			'.$tbody.'
		</table>';
	return $r;
}

function settings_aboutus(){
	global $db, $dir_folder;
	$dir = $dir_folder['other'];//'im/landing/';

	if ($_POST['save']) {
		$missing_field = 0;
		if (trim($_POST['aboutus_details']) == '') {
			++$missing_field;
		}
		if ($missing_field > 0) {
			$opt['missing_field'] = 'Missing Required Fields';
			echo json_encode($opt);
			exit;
		}

		$param_sql = array(
			'table' => 'sf_aboutus',
			'cols' => array(
				'aboutus_details', '%s', $_POST['aboutus_details'],
				'updated', '', 'NOW()',
				'updated_by', '%d', $_SESSION[SESSION_L]
			),
		);
		
		if ($_FILES['aboutus_img']) {
			$extension = fw_file_extension($_FILES['aboutus_img']['name']);
			$param_sql['cols'][] = 'aboutus_img';
			$param_sql['cols'][] = '%s';
			$param_sql['cols'][] = $extension;
		}

		$sql = 'SELECT COUNT(1) FROM sf_aboutus';
		$cn = $db->GetOne($sql);
		
		if ($_POST['id'] > 0 && $cn > 0) {
			$id_aboutus = $_POST['id'];
			//edit
			$param_sql['where'] = SQL('id_aboutus=%d', $id_aboutus);
			db_query($param_sql);
		} else {
			//add new
			$param_sql['cols'][] = 'added';
			$param_sql['cols'][] = '';
			$param_sql['cols'][] = 'NOW()';
			$param_sql['cols'][] = 'added_by';
			$param_sql['cols'][] = '%d';
			$param_sql['cols'][] = $_SESSION[SESSION_L];
			$id_aboutus = db_query($param_sql);
		}
		if ($_FILES['aboutus_img']) {
			$name = $dir . 'aboutus.'.$extension;
			move_uploaded_file($_FILES['aboutus_img']['tmp_name'], $name);
		}
		
		$opt['completed'] = 1; 
		echo json_encode($opt);
		exit;
	}

	if (!$_REQUEST['id'] && !$_REQUEST['link']) {
		$_REQUEST['id'] = 1;
		$_REQUEST['link'] = 'edit';
	}
	
	if ($_REQUEST['link'] == 'edit' || $_REQUEST['link'] == 'add') {
		$content = settings_aboutus_form();
	} else {
		$content = settings_aboutus_list();
	}
	$r = '
	<div id="page">
		'.bg_page().'
		<div id="page-title" style="margin-top: -200px; transition: all 2s cubic-bezier(0.25, 0.1, 0.25, 1) 2s;">
			About us
		</div>
		<div class="settings" id="page-content" style="width: 934px; left: -100px; opacity: 0; transition: all 2s cubic-bezier(0.25, 0.1, 0.25, 1) 0s ;">
			'.$content.'
		</div>
	</div>
	<script type="text/javascript">
		$(document).ready(function(){
			page_style();
		});
	</script>';
	return settings().$r;
}

function settings_aboutus_form() {
	global $db, $dir_folder;
	$dir = $dir_folder['other'];//'im/landing/';
	if ($_REQUEST['link'] == 'edit') {
		$sql = SQL('
		SELECT 	
			id_aboutus, 
			aboutus_details, 
			aboutus_img, 
			added, 
			added_by, 
			updated, 
			updated_by, 
			removed, 
			removed_by
		FROM 
			sf_aboutus 
		WHERE
			removed IS NULL
			AND id_aboutus=%d', $_REQUEST['id']);
		$row = $db->GetRow($sql);
	
		$im_file = $dir . 'aboutus.' . $row['aboutus_img'];
		$show_im_file = '';
		if (file_exists($im_file)){
			$show_im_file .= '<img src="'.$im_file.'" width="200" style="border: 1px solid #999; margin-top: 10px;"/>';
		}
	}
	
	$r .= '<form id="form_settings" method="post" action="index.php?mod=settings&view=aboutus&ajax=1" enctype="multipart/form-data">
			<table style="float: left;">
				<tr><td>Img-aboutus*[350x400]:<br/>'.form_file('aboutus_img', array()).'<br/>'.$show_im_file.'</td></tr>
				<tr><td>Details*:<br/>'.form_textarea('aboutus_details', array('default' => $row['aboutus_details'])).'</td></tr>
				<tr><td align="right"><input type="hidden" name="id" value="'.$_REQUEST['id'].'"/>
					<input type="button" name="cancel" value="cancel" onclick="window.location=\'index.php?mod=settings&view=aboutus\'"/>
					<input type="submit" name="save" value="save"/>
				</td></tr>
			</table>
		</form>
		<script type="text/javascript">
			$(document).ready(function(){
				$("input[name=save]").click(function(){
					$("textarea[name=aboutus_details]").val(tinyMCE.get("aboutus_details").getContent());
				});
				$("input[type=submit], input[type=button]").button();
				$("#form_settings").ajaxForm({
					dataType:  "json",
					success:   processJson
				});
			});
			
			function processJson(data) {
				if (data.completed == 1) {
					alert("updated");
					window.location="index.php?mod=settings&view=aboutus";
				} else {
					alert(data.missing_field);
				}
			}
		</script>
		';
	return $r;
}

function settings_aboutus_list() {
	global $db;
	return;
}

function settings_contactus(){
	global $dir_folder, $db;
	
	if ($_POST['save']) {
		$missing_field = 0;
		if (trim($_POST['contactus_details']) == '') {
			++$missing_field;
		}
		if ($missing_field > 0) {
			$opt['missing_field'] = 'Missing Required Fields';
			echo json_encode($opt);
			exit;
		}

		$param_sql = array(
			'table' => 'sf_contactus',
			'cols' => array(
				'contactus_details', '%s', $_POST['contactus_details'],
				'updated', '', 'NOW()',
				'updated_by', '%d', $_SESSION[SESSION_L]
			),
		);
		
		$sql = 'SELECT COUNT(1) FROM sf_contactus';
		$cn = $db->GetOne($sql);

		if ($_POST['id'] > 0 && $cn > 0) {
			$id_contactus = $_POST['id'];
			//edit
			$param_sql['where'] = SQL('id_contactus=%d', $id_contactus);
			db_query($param_sql);
		} else {
			//add new
			$param_sql['cols'][] = 'added';
			$param_sql['cols'][] = '';
			$param_sql['cols'][] = 'NOW()';
			$param_sql['cols'][] = 'added_by';
			$param_sql['cols'][] = '%d';
			$param_sql['cols'][] = $_SESSION[SESSION_L];
			$id_contactus = db_query($param_sql);
		}
		
		$opt['completed'] = 1; 
		echo json_encode($opt);
		exit;
	}

	if (!$_REQUEST['id'] && !$_REQUEST['link']) {
		$_REQUEST['id'] = 1;
		$_REQUEST['link'] = 'edit';
	}
	
	if ($_REQUEST['link'] == 'edit' || $_REQUEST['link'] == 'add') {
		$content = settings_contactus_form();
	} else {
		$content = settings_contactus_list();
	}
	$r = '
	<div id="page">
		'.bg_page().'
		<div id="page-title" style="margin-top: -200px; transition: all 2s cubic-bezier(0.25, 0.1, 0.25, 1) 2s;">
			Contact us
		</div>
		<div class="settings" id="page-content" style="width: 934px; left: -100px; opacity: 0; transition: all 2s cubic-bezier(0.25, 0.1, 0.25, 1) 0s ;">
			'.$content.'
		</div>
	</div>
	<script type="text/javascript">
		$(document).ready(function(){
			page_style();
		});
	</script>';
	return settings().$r;
}

function settings_contactus_form() {
	global $db;
	
	if ($_REQUEST['link'] == 'edit') {
		$sql = SQL('
		SELECT 	
			id_contactus, 
			contactus_details, 
			added, 
			added_by, 
			updated, 
			updated_by, 
			removed, 
			removed_by
		FROM 
			sf_contactus
		WHERE
			removed IS NULL
			AND id_contactus=%d', $_REQUEST['id']);
		$row = $db->GetRow($sql);
	}
	
	$r .= '<form id="form_settings" method="post" action="index.php?mod=settings&view=contactus&ajax=1" enctype="multipart/form-data">
			<table style="float: left;">
				<tr><td>Details*:<br/>'.form_textarea('contactus_details', array('default' => $row['contactus_details'])).'</td></tr>
				<tr><td align="right"><input type="hidden" name="id" value="'.$_REQUEST['id'].'"/>
					<input type="button" name="cancel" value="cancel" onclick="window.location=\'index.php?mod=settings&view=contactus\'"/>
					<input type="submit" name="save" value="save"/>
				</td></tr>
			</table>
		</form>
		<script type="text/javascript">
			$(document).ready(function(){
				$("input[name=save]").click(function(){
					$("textarea[name=contactus_details]").val(tinyMCE.get("contactus_details").getContent());
				});
				$("input[type=submit], input[type=button]").button();
				$("#form_settings").ajaxForm({
					dataType:  "json",
					success:   processJson
				});
			});
			
			function processJson(data) {
				if (data.completed == 1) {
					alert("updated");
					window.location="index.php?mod=settings&view=contactus";
				} else {
					alert(data.missing_field);
				}
			}
		</script>
		';
	return $r;
}

function settings_contactus_list() {
	global $db;
	return;
}
